package com.cqcet.wenchuang.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.cqcet.wenchuang.common.R;
import com.cqcet.wenchuang.dto.OrderDto;
import com.cqcet.wenchuang.entity.OrderDetails;
import com.cqcet.wenchuang.entity.Orders;
import com.cqcet.wenchuang.service.OrderDetailsService;
import com.cqcet.wenchuang.service.OrdersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * 订单
 */
@Slf4j
@Api(tags = "订单管理接口")
@RestController
@RequestMapping("/orders")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrderDetailsService orderDetailsService;

    /**
     * 直接购买
     *
     * @param orderDto
     * @return
     */
    @ApiOperation(value = "直接下单", notes = "必要:goodsId,userId,quantity,addressId,可选:remark")
    @PostMapping
    public R<String> save(@RequestBody OrderDto orderDto) {
        ordersService.saveWithDetails(orderDto);
        return R.success("下单成功");
    }

    /**
     * 购物车下单
     *
     * @return
     */
    @ApiOperation(value = "从购物车下单", notes = "必要:shoppingCartIds(集合),addressId")
    @PostMapping("/fromShoppingCart")
    public R<String> saveFromShoppingCart(@RequestBody OrderDto orderDto) {
        ordersService.saveFromShoppingCart(orderDto);
        return R.success("下单成功");
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
//    @PreAuthorize("hasAuthority('system:manage:users')")
    @ApiOperation("删除订单")
    @DeleteMapping("/{id}")
    public R<String> remove(@PathVariable Long id) {
        //设置状态为删除
        LambdaUpdateWrapper<Orders> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ObjectUtils.isNotEmpty(id), Orders::getId, id);
        updateWrapper.set(Orders::getStatus, 4);
        ordersService.update(updateWrapper);
        return R.success("删除成功");
    }

    /**
     * 支付
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "支付订单", notes = "将订单状态修改为已支付")
    @PutMapping("/pay/{id}/{payMethod}")
    public R<String> pay(@PathVariable Long id, @PathVariable Integer payMethod) {
        LambdaUpdateWrapper<Orders> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ObjectUtils.isNotEmpty(id), Orders::getId, id);
        //订单状态改为已支付
        updateWrapper.set(Orders::getStatus, 2);
        //设置支付方式
        updateWrapper.set(ObjectUtils.isNotEmpty(payMethod), Orders::getPayMethod, payMethod);
        //支付时间
        updateWrapper.set(Orders::getCheckoutTime, LocalDateTime.now());
        ordersService.update(updateWrapper);
        return R.success("支付成功");
    }

    /**
     * 签收
     *
     * @return
     */
    @ApiOperation(value = "签收订单", notes = "将订单状态修改为已签收")
    @PutMapping("/signFor/{id}")
    public R<String> signFor(@PathVariable Long id) {
        LambdaUpdateWrapper<Orders> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ObjectUtils.isNotEmpty(id), Orders::getId, id);
        //订单状态改为已支付
        updateWrapper.set(Orders::getStatus, 3);
        ordersService.update(updateWrapper);
        return R.success("签收成功");
    }
}